package com.zyf.entity;

import com.zyf.service.serviceImpl.serviceImpl;
import com.zyf.util.DButil;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;


public class window {

    public Scanner input = new Scanner(System.in);
    public serviceImpl servicee = new serviceImpl();

    public void showTitle() {
        System.out.println("\t欢迎使用图书管理系统");
    }

    public int showMenu() {
        System.out.println("---------菜单---------");
        System.out.println("----1:列出所有图书");
        System.out.println("----2:通过书名搜索");
        System.out.println("----3:通过作者搜索");
        System.out.println("----4:通过存放位置搜索");
        System.out.println("----5:创建新图书");
        System.out.println("----6:通过ID删除图书");
        System.out.println("----7:修改图书信息");
        System.out.println("----0:退出");
        System.out.println("----请选择");
        return getInt(this.input);
    }

    public int showInnerMenu() {
        System.out.println("--------options-------");
        System.out.println("----1:设置书名");
        System.out.println("----2:设置作者");
        System.out.println("----3:设置存放位置");
        System.out.println("----4:更新入库时间");
        System.out.println("----5:回到主菜单");
        System.out.println("----请选择:");
        return getInt(this.input);
    }

    public String getString(Scanner input) {
        return input.nextLine();
    }

    public int getInt(Scanner input) {
        int n = -1;
        while (n < 0) {
            try {
                n = input.nextInt();
                input.nextLine();
            } catch (InputMismatchException e) {
                e.printStackTrace();
                System.out.println("输入有误,请重新输入:");
            }
        }
        return n;
    }

    public java.sql.Date processDate(String enterdate) {
        java.sql.Date sqldate;
        if (enterdate.length() == 0) {
            sqldate = DButil.getSqlTime();
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                sqldate = new java.sql.Date(sdf.parse(enterdate).getTime());
            } catch (ParseException e) {
                e.printStackTrace();
                System.out.println("输入的日期无效,使用当前时间代替");
                sqldate = DButil.getSqlTime();
            }
        }
        return sqldate;
    }

    public void printTip(int n, String s) {
        if (n < 0) {
            System.out.println(s + "失败");
        } else {
            System.out.println(s + "成功");
        }
    }

    public book createBook() {
        System.out.print("请输入书名:");
        String bookName = getString(this.input);
        System.out.print("请输入作者:");
        String bookAuther = getString(this.input);
        System.out.print("请输入价格:");
        float price = this.input.nextFloat();
        input.nextLine();
        System.out.print("请输入存放位置:(例:A3-1234)");
        String location = getString(this.input);
        System.out.print("请输入入库时间:(例:2017-01-01 12:12:12)留空采用当前时间");
        String enterdate = getString(this.input);
        java.sql.Date sqldate = processDate(enterdate);

        System.out.println("input finished");
        return new book(price, bookName, bookAuther, location, sqldate);
    }

    public void showBooks(List<book> list) {

        System.out.println("-------books--------");
        book tempbook;
        if (list.isEmpty()) {
            System.out.println("查询不到书本");
        } else {
            for (int i = 0; i < list.size(); i++) {
                tempbook = list.get(i);
                tempbook.showInfo();
            }
        }
    }

    public void listAllBook() {
        showBooks(servicee.listAllbook());
    }

    public void findByName() {
        System.out.println("请输入书名:");
        String bookName = getString(input);
        showBooks(servicee.findBookByName(bookName));
    }

    public void findByAuther() {
        System.out.println("请输入作者:");
        String bookAuther = getString(input);
        showBooks(servicee.findBookByAuther(bookAuther));
    }

    public void findByLocation() {
        System.out.println("请输入存放位置:");
        String location = getString(input);
        showBooks(servicee.findBookByLocation(location));
    }

    public void newBook() {
        int n = -1;
        n = servicee.insertNewBook(createBook());
        printTip(n, "创建");
    }

    public void deleteBook() {
        int n = -1;
        int id = getInt(input);
        n = servicee.deleteBookById(id);
        printTip(n, "删除");
    }

    public void setName(int id) {
        System.out.println("请输入图书名字");
        String name = getString(input);
        int n = servicee.setName(id, name);
        printTip(n, "修改");
    }

    public void setAuther(int id) {
        System.out.println("请输入图书作者");
        String auther = getString(input);
        int n = servicee.setAuther(id, auther);
        printTip(n, "修改");
    }

    public void setLocation(int id) {
        System.out.println("请输入存放位置");
        String location = getString(input);
        int n = servicee.setLocation(id, location);
        printTip(n, "修改");
    }

    public void refreashDate(int id) {
        System.out.println("请输入入库时间,默认为当前");
        String enterdate = getString(input);
        java.sql.Date sqldate = processDate(enterdate);
        int n = servicee.updateDate(id, sqldate);
        printTip(n, "修改");
    }

    public void changeInfo() {

        System.out.println("请输入书本的ID");
        int id = getInt(input);
        servicee.findBookById(id).showInfo();
        int choose = showInnerMenu();
        int flag = -1;
        while (flag < 0) {
            switch (choose) {
                case 1:
                    setName(id);
                    flag = 1;
                    break;
                case 2:
                    setAuther(id);
                    flag = 1;
                    break;
                case 3:
                    setLocation(id);
                    flag = 1;
                    break;
                case 4:
                    refreashDate(id);
                    flag = 1;
                    break;
                case 5:
                    flag = 1;
                default:
                    System.out.println("请重试");

            }
        }
    }

}